package spring.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>登录验证的拦截器</p>
 * @author huangqiankun
 * @date 2020年6月4日下午6:24:37
 */
public class LoginInterceptor implements HandlerInterceptor {
	
	/**
	 * 执行控制器之后且在完成渲染视图后调用此方法
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}

	/**
	 * 执行控制器之后且在渲染视图前调用此方法
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	/**
	 * 执行控制器之前调用此方法
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		boolean isLogin = false;
		/**
		 * 判断是否登录
		 */

		if(!isLogin) {



			response.getWriter().print("<script>top.window.location.href='/admin/login'</script>");
			return false;
		}
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}
	
}
